<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard-editor-container {
  padding: 18px 22px 22px 22px;
  background-color: rgb(240, 242, 245);
  margin-bottom: 80px;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}
</style>
<template>
  <div class="dashboard-container">
    <div class="dashboard-editor-container">
      <el-card class="box-card" v-if="crud.optPageShow.listPage">
        <div slot="header" class="clearfix">
          <span>定时任务实例列表</span>
        </div>
        <!--工具栏-->
        <div class="head-container">
          <crudOperation :defaultQueryForm="defaultQueryForm" :moreQueryList="moreQueryList"/>
        </div>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;"
                  @select="crud.selectChange"
                  @select-all="crud.selectAllChange" @selection-change="crud.selectionChangeHandler"
                  @filter-change="crud.handleFilterChange"
                  :header-cell-style="{ background: 'rgba(52, 125, 225, 0.1)', color: '#404248' }" border stripe>
          <el-table-column type="selection" width="55"/>
          <el-table-column v-if="false" prop="pkId" label="id"/>
          <el-table-column v-if="columns.visible('taskCode')" :show-overflow-tooltip="true" min-width="100"
                           prop="taskCode"
                           label="任务编码"/>
          <el-table-column v-if="columns.visible('taskName')" :show-overflow-tooltip="true" min-width="100"
                           prop="taskName"
                           label="任务名称"/>
          <el-table-column v-if="columns.visible('cronExpress')" :show-overflow-tooltip="true" min-width="100"
                           prop="cronExpress"
                           label="CRON表达式"/>
          <el-table-column v-if="columns.visible('shellPath')" :show-overflow-tooltip="true" min-width="100"
                           prop="shellPath"
                           label="shell文件"/>
          <el-table-column v-if="columns.visible('extParam')" :show-overflow-tooltip="true" min-width="100"
                           prop="extParam"
                           label="外部参数"/>
          <el-table-column v-if="columns.visible('appointAgentName')" :show-overflow-tooltip="true" min-width="100"
                           prop="appointAgentName" label="执行节点"/>
          <el-table-column v-if="columns.visible('execStTs')" :show-overflow-tooltip="true" width="140" prop="execStTs"
                           label="执行开始时间"/>
          <el-table-column v-if="columns.visible('execEndTs')" :show-overflow-tooltip="true" width="140"
                           prop="execEndTs"
                           label="执行结束时间"/>
          <el-table-column v-if="columns.visible('costTs')" :show-overflow-tooltip="true" width="100" prop="costTs"
                           label="耗时(秒)"/>
          <el-table-column v-if="columns.visible('execStatus')" prop="execStatus" label="执行状态 " width="120"
                           :filters="cronexecStatusDict" key="execStatus"
                           :filter-method="filterExecStatus" filter-placement="bottom-end" :filter-multiple="false"
                           column-key="execStatus">
            <template slot-scope="scope">
              <el-tag size="medium" :type="formatterExecStatusType(scope.row.execStatus)">
                {{ formatterExecStatus(scope.row.execStatus) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="执行失败原因" v-if="columns.visible('execFailReason')" prop="execFailReason">
            <template slot-scope="scope">
              <el-button @click="lookErrorInf(scope.row)" v-if="'2' == scope.row.execStatus" type="text" size="small">
                查看原因
              </el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-card>
    </div>
    <el-dialog title="失败原因" :visible.sync="dialogVisible" width="30%">
      <div style="max-height: 300px;overflow-y: auto">{{ errorInf }}</div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cronTaskApi from '@/api/cronTask/cronTask'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import crudOperation from '@crud/extend/components/CRUD.OnlyLookOperation'

const queryList = [{
  'cloumValue': 'taskCode',
  'cloumName': '任务编码'
}, {
  'cloumValue': 'taskName',
  'cloumName': '任务名称'
}, {
  'cloumValue': 'batchTag',
  'cloumName': '批次标签'
}];
const defaultCrud = CRUD({
  title: '任务实例',
  queryList: queryList,
  url: '/api/cronTask',
  queryOnPresenterCreated: false,
  sort: 'sort,asc',
  crudMethod: {
    ...cronTaskApi
  }
})
const defaultForm = {}
export default {
  name: 'appCron',
  components: {
    crudOperation
  },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  filters: {
    formatTime: function (value) {
      if (value != '') {
        return value.slice(0, 19).replace('T', ' ')
      }
    }
  },
  dicts: ['exec_status'],
  data() {
    return {
      dialogVisible: false,
      errorInf: '',
      rerunTask: false,
      permission: {
        moreQuery: ['app', 'appCron:moreQuery']
      },
      defaultQueryForm: {
        taskCode: null,
        taskName: '',
        cronExpress: null,
        batchTag: null,
        appointAgentCode: null,
        execStatus: '',
        startTime: [],
        endTime: [],
        taskCodes: null
      },
      queryOnPresenterCreated: false,
      moreQueryList: [{
        cloumLable: 'taskCodes',
        name: "任务编码",
        type: 'textarea',
        placeholder: '可输入多个任务编码用,隔开',
        defaultValue: null
      },
        {
          cloumLable: 'taskName',
          name: "任务名称",
          type: 'input',
          placeholder: '',
          defaultValue: null
        },
        {
          cloumLable: 'cronExpress',
          name: "CRON表达式",
          type: 'input',
          placeholder: '',
          defaultValue: null
        },
        {
          cloumLable: 'execStatus',
          name: "执行状态",
          type: 'radio',
          placeholder: '',
          defaultValue: null,
          radioList: [{
            label: '',
            value: '全部'
          },
            {
              label: '2',
              value: '执行失败'
            },
            {
              label: '1',
              value: '执行成功'
            },
            {
              label: '3',
              value: '执行中'
            }
          ]
        },
        {
          cloumLable: 'startTime',
          cloumLableType: 'startTimeType',
          name: "执行开始时间",
          type: 'timeFramePicker',
          placeholder: '',
          defaultValue: null
        },
        {
          cloumLable: 'endTime',
          cloumLableType: 'endTimeType',
          name: "执行结束时间",
          type: 'timeFramePicker',
          placeholder: '',
          defaultValue: null
        },
      ],
      cronexecDict: [],
      cronexecStatusDict: []
    }
  },
  computed: {},
  created() {
    this.initQry()
    this.cronexecDict = this.dict.exec_status;
  },
  watch: {
    cronexecDict() {
      this.$nextTick(() => {
        this.cronexecDict.forEach(item => {
          var obj = {
            text: item.label,
            value: item.value
          }
          this.cronexecStatusDict.push(obj);
        })
      })
    }
  },
  methods: {
    initQry() {
      this.crud.query.pkId = this.$route.query.pkId
      this.crud.toQuery()
    },
    filterExecStatus(value, row) {
      return row.execStatus === value;
    },
    lookErrorInf(data) {
      this.errorInf = data.execFailReason;
      this.dialogVisible = true;
    },
    formatterExecStatusType(val) {
      const type = ['', 'success', 'warning', 'primary', 'danger']
      return type[val]
    },
    formatterExecStatus(val) {
      return this.dict.label.exec_status[val]
    },
  }
}
</script>
